﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceProcess;
using System.Threading;
using System.Configuration;
using System.Diagnostics;
using Microhard.Core.Mail;

namespace Microhard.Core
{
    public class MHBaseWinService : ServiceBase
    {
        //用于循环遍历数据
        private static Timer timer;
        //遍历数据的间隔毫秒
        private string _interval;

        protected virtual void InitBaseData()
        {
            _interval = ConfigurationManager.AppSettings["QueryInterval"];
            timer = new Timer(ExeProcess, null, int.Parse(_interval), Timeout.Infinite);
        }

        private void SendErrorMail(string title, string message)
        {
            try
            {
                string mailModel = System.Configuration.ConfigurationManager.AppSettings["MailModel"];
                string fromMail = System.Configuration.ConfigurationManager.AppSettings["FromMail"];
                string aName = System.Configuration.ConfigurationManager.AppSettings["MailAccountName"];
                string aPwd = System.Configuration.ConfigurationManager.AppSettings["MailAccountPWD"];
                string smtp = System.Configuration.ConfigurationManager.AppSettings["MailSMTP"];
                string port = System.Configuration.ConfigurationManager.AppSettings["MailPORT"];
                string alerts = System.Configuration.ConfigurationManager.AppSettings["AlertMails"];
                bool dossl = false;
                if (mailModel == "SSL")
                {
                    dossl = true;
                }
                string[] to = alerts.Split(new char[] { ',' });
                foreach (var t in to)
                {
                    if (dossl)
                    {
                        MailTool.SendHTMLMailWithSSL(fromMail, t, title, message, smtp, port, aName, aPwd);
                    }
                    else
                    {
                        MailTool.SendMailWithSMTP(fromMail, t, title, message, smtp, port, aName, aPwd);
                    }
                }
            }
            catch (Exception ex)
            {

            }
        }

        private void ExeProcess(object state)
        {
            bool canrun = true;
            Stopwatch watch = new Stopwatch();
            watch.Start();

            Log.Info("MHBaseWinService.ExeProcess", "开始遍历" + DateTime.Now);
            try
            {
                this.GetData(state);
            }
            catch (Exception ex)
            {
                SendErrorMail("服务运行错误", ex.Message);
                Log.Error("ExeProcess", ex);
                timer.Dispose();
                canrun = false;
            }

            watch.Stop();
            if (canrun)
            {
                timer.Change(int.Parse(_interval) + watch.ElapsedMilliseconds, Timeout.Infinite);
            }
            Log.Info("MHBaseWinService.ExeProcess", "遍历结束" + DateTime.Now);
        }

        protected virtual void GetData(Object state)
        {
            return;
        }

        protected override void OnStart(string[] args)
        {
            this.InitBaseData();
            Log.Info("MHBaseWinService.OnStart", "服务开始" + DateTime.Now);
        }
        protected override void OnPause()
        {
            timer.Dispose();
            Log.Info("MHBaseWinService.OnStop", "服务暂停" + DateTime.Now);
        }

        protected override void OnContinue()
        {
            timer.Dispose();
            Log.Info("MHBaseWinService.OnStop", "服务重新启动" + DateTime.Now);
        }

        protected override void OnStop()
        {
            timer.Dispose();
            Log.Info("MHBaseWinService.OnStop", "服务停止" + DateTime.Now);
        }
    }
}
